import md5 from './md5'
import {serialize,deserialization} from './fastJson'

class MIMCSocket {
  constructor(uri) {
    var wsUri = uri;
    var loginState = false;
    var resource = getResource();
    var websocket;
    var intervalId;
    var msgHandler, disconnectHandler;

    //注册消息监听
    this.registerMsgHandler = function (func) {
      msgHandler = func;
    };

    //disconnect(),回调函数无参数
    this.registerDisconnectHandler = function (func) {
      disconnectHandler = func;
    };

    this.isConnected = function () {
      return websocket.readyState === 1
    };

    this.isLogin = function () {
      return loginState;
    };

    function getResource () {
      try
      {
        resource = md5(window.navigator.userAgent);
      }
      catch (err)
      {
        resource = "resWeb";
        console.log("md5 failed,err=" + err);
      }
      return resource;
    }

    function initWebsocket () {
      websocket = new WebSocket(wsUri);
      websocket.onopen = function (evt) {
        onOpen(evt);
      };
      websocket.onclose = function (evt) {
        onClose(evt);
      };
      websocket.onmessage = function (evt) {
        onMessage(evt);
      };
      websocket.onerror = function (evt) {
        onError(evt);
      };
    }

    function onOpen (evt) {
      console.log(evt);
      MIMCConnect();
    }

    function onClose (evt) {
      if (disconnectHandler !== undefined && disconnectHandler !== null)
      {
        disconnectHandler(evt);
      }
      else
      {
        console.log("DisconnHandler is not registered.");
      }
      loginState = false;
      clearInterval(intervalId);
    }

    function onMessage (evt) {
      if (msgHandler !== undefined && msgHandler !== null)
      {
        try
        {
          resource = msgHandler(deserialization(evt));
        }
        catch (err)
        {
          console.log("deserialization failed,err=" + err);
        }
      }
      else
      {
        console.log("msgHandler is not registered");
      }
    }

    function onError (evt) {
      console.log(evt);
      clearInterval(intervalId);
    }

    function sendWSMessage (wsMessage) {
      console.log("发送消息",wsMessage,wsMessage.length);
      if (wsMessage.length > 10 * 1024)
      {
        console.log("packet len > 10k");
        throw "packet len > 10k";
      }
      try
      {
        if(typeof (wsMessage) == 'string')
        {
          websocket.send(wsMessage);
        }else{
          websocket.send(serialize(wsMessage));
        }
      } catch (err)
      {
        console.log("websocket send err=" + err);
      }
    }

    this.closeWebsocket = function () {
        try {
            websocket.close();
        } catch(err) {
            console.log("websocket close err=" + err);
        }
    }

    this.login = function () {
      initWebsocket();
    };

    this.logout = function () {
      // sendWSMessage();
    };

    function sendPing () {
      sendWSMessage('P');
      intervalId = setInterval(function () {
        sendWSMessage('P');
      }, 10000);
    }

    function MIMCConnect () {
      clearInterval(intervalId);
      sendPing();
    }
  }
}

export default MIMCSocket;
